package com.cityfood.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cityfood.mapper.CfUserMapper;
import com.cityfood.pojo.CfCity;
import com.cityfood.pojo.CfUser;
import com.cityfood.pojo.CfUserExample;
import com.cityfood.pojo.CfUserExample.Criteria;
import com.cityfood.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class UserServiceImpl implements UserService {
	
	@Autowired
	private CfUserMapper userMapper;

	/**
	 * 根据用户id展现用户可视信息返回包装类
	 */
	@Override
	public CfUser getUserByUserId(Integer userId) {
		return userMapper.selectByPrimaryKey(userId);
	}
	

	@Override
	public List<CfUser> getUserLikePhone(String phone) {
		CfUserExample example = new CfUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneLike(phone+"%");
		List<CfUser> list = userMapper.selectByExample(example);
		return list;
	}
	

	@Override
	public PageInfo<CfUser> getUserLikePhonePage(String phone, Integer pageNum) {
		
		PageHelper.startPage(pageNum, 20);
		
		CfUserExample example = new CfUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneLike(phone+"%");
		List<CfUser> list = userMapper.selectByExample(example);
		
		//5.获取分页信息
		PageInfo<CfUser> info = new PageInfo<>(list);
		return info;
	}
	
	
	@Override
	public int updateUserBindPhone(CfUser user) {
		int i = userMapper.updateByPrimaryKeySelective(user);
		return i;
	}

	@Override
	public CfUser getUserByPhone(String phone) {
		CfUserExample example = new CfUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		List<CfUser> list = userMapper.selectByExample(example);
		if (list.size()>0) {
			CfUser user = list.get(0);
			return user;
		}
		return null;
	}

	@Override
	public CfUser findUserByOpenid(String openid) {
		CfUserExample example = new CfUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andOpenidEqualTo(openid);
		List<CfUser> list = userMapper.selectByExample(example);
		if (list.size()>0) {
			CfUser user = list.get(0);
			return user;
		}
		return null;
	}

	@Override
	public int insertUser(CfUser user) {
		int i = userMapper.insertSelective(user);
		return i;
	}


	@Override
	public int updateUser(CfUser user) {
		int i = userMapper.updateByPrimaryKeySelective(user);
		return i;
	}


	@Override
	public List<CfUser> getUserLikeNickName(String nickName) {
		CfUserExample example = new CfUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andNicknameLike("%"+nickName+"%");
		List<CfUser> list = userMapper.selectByExample(example);
		return list;
	}

}
